package com.juben.svr.service.impl;

import com.juben.svr.domain.*;
import com.juben.svr.domain.Enum.TypeAndtheme;
import com.juben.svr.mapper.UWorkmapper;
import com.juben.svr.service.UworkService;
import com.juben.svr.service.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.rmi.server.UID;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
public class UWorkimpl implements UworkService{
    @Autowired
    private UWorkmapper UserWorkmapper;

    private ScriptLabel scriptLabel;
    @Autowired
    private TimeService timeService;

//    @Override
    public UWorks getworkByID(String WID){
        System.out.println("WID2:"+WID);
        UWorks uWorks = UserWorkmapper.getworkByID(WID);
        System.out.println("查询情况："+UserWorkmapper.getworkByID(WID));
        System.out.println("uWorks:"+uWorks);
        return uWorks;
    }
    public boolean createUWorks(UWorks uWorks){
        System.out.println("createUWorks");
        System.out.println("createUWorks:"+uWorks);
        System.out.println("uWorks.setWimage(\"img0.png\")"+uWorks.getWimage());
        System.out.println("uWorks.getWimage()"+uWorks.getWimage());
        if (uWorks.getWimage() == null){
            uWorks.setWimage("img0.png");
            System.out.println("uWorks.getWimage()"+uWorks.getWimage());
        }
        String WID = IdUtil.getId();

        uWorks.setWID(WID);
        //获取到你跟前时间
        String nowDate = timeService.getCurrentDateTime().toString();
        String Wdate = nowDate;
        System.out.println("nowDate:"+nowDate);
        uWorks.setWnowdate(nowDate);
        uWorks.setWdate(Wdate);
        System.out.println("插入jb_uworks前，uWorks:"+uWorks);
        // 插入到 jb_uworks 表
        UserWorkmapper.insertUWorks(uWorks);
        System.out.println("插入jb_uworks成功");
        // 获取插入之后的 UID （假设你已经获取到这个值，通常是用结合设置 Guid，缓冲值）
//        Integer WID = uWorks.getWID();
        //String WID = IdUtil.getId();
        System.out.println("插入jb_uworks成功，WID为：" + WID);
        Set<Integer> labelIds = uWorks.getLabelIds();
        System.out.println("插入script_label表，labelIds为：" + labelIds);
        for (Integer BID : labelIds) {
            System.out.println("插入script_label表，BID为：" + BID);
            System.out.println("插入script_label表，WID为：" + WID);
            ScriptLabel scriptLabel = new ScriptLabel(WID, BID);
            UserWorkmapper.insertScriptLabel(scriptLabel);
        }

        // 插入到 script_label 表

        return true;
    }

    //查询剧本类型列表
    public List<jb_types> getTypeList(){
        return UserWorkmapper.getTypeList();
    }

    //查询所有标签(题材）
    public List<Label> getLabelList(){
        return UserWorkmapper.getLabelList();
    }


    // 根据用户id查询剧本列表
    public List<UWorks> getUWorksByUID(Integer UID){
        return UserWorkmapper.getUWorkListByUID(UID);
    }

    public boolean updateUWorks(UWorks uWorks){
        System.out.println("updateUWorks");
        System.out.println("获取的uWorks："+uWorks);
        String Wnowdate = timeService.getCurrentDateTime().toString();
        uWorks.setWnowdate(Wnowdate);
        return UserWorkmapper.updateUWorks(uWorks);
    }

//    public boolean setPnameByID(String WID, String Pname){
//        return UserWorkmapper.setPnameByID(WID,Pname);
//    }

    // 根据 WID 查询剧本题材集
    public List<Label> getLabelIdsByWID(String WID){
        List<Label> labels = UserWorkmapper.getLabelListByWID(WID);
        return labels;
    }

    //根据剧本类型id ，查询剧本类型
    public String getTypeNameByID(Integer themeId){
        return UserWorkmapper.getWorkType(themeId);
    }

    //修改剧本合同内容
    public boolean updateUWorksByContract(UWorks uWorks){
        System.out.println("updateUWorksByReview");
        System.out.println("获取的uWorks："+uWorks);
        return UserWorkmapper.updateUWorksByContract(uWorks);
    }

    //根据剧本类型id ，查询同类型剧本列表
    public List<UWorks> getUWorkListByTypeId(Integer themeId){
        return UserWorkmapper.getUWorkListByTypeId(themeId);
    }

    //通过剧本名称模糊搜索
    public  List<UWorks> getUWorkListByWName(String Wname){
        return UserWorkmapper.getUWorkListByName(Wname);
    }

    public List<UWorks> getUWorkList(TypeAndtheme typeAndtheme){
        return UserWorkmapper.getUWorkList(typeAndtheme);
    }

    //初始化交易合同
    public Boolean insertTransaction(Transaction transaction){
        System.out.println("服务===============");
        Boolean i =false;
        i= UserWorkmapper.insertTransaction(transaction);
        System.out.println("插入交易合同结果："+i);
        return i;
    }

    //判断合同是否存在？
    public Boolean TransactionExite(Transaction transaction){
        System.out.println("Transaction:"+transaction);
        System.out.println("i:"+UserWorkmapper.TransactionExite(transaction));
        Transaction i = UserWorkmapper.TransactionExite(transaction);
        //判断获取到的条数
        if (i != null){
            return true;
        }else {
            return false;
        }
    }
    public Transaction getTransaction(Transaction transaction){
        return UserWorkmapper.TransactionExite(transaction);
    }

    //添加数字签名和日期
    public Boolean setPartyAndTime(Transaction transaction){
        return UserWorkmapper.setPartyAndTime(transaction);
    }

    public Boolean setTransaction(Transaction transaction){
        System.out.println("setTransaction_______:"+transaction);
        return UserWorkmapper.setTransaction(transaction);
    }


}
